<?php

namespace App\Jobs;

use App\Services\ProxyService;
use Illuminate\Support\Facades\Log;

class AddIPWebCustomer extends Job
{

    protected $user_id;

    /**
     * 任务可尝试次数
     *
     * @var int
     */
    public $tries = 5;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_id)
    {
        $this->user_id = $user_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(ProxyService $proxyService)
    {
        Log::info('创建客户队列 user_id：' . $this->user_id);
        $result = $proxyService->addUser($this->user_id);
        if ($result['code'] !== 0) {
            if ($this->attempts() >= $this->tries) {
                Log::error("创建客户队列 user_id: {$this->user_id} 达到最大重试次数，仍然失败，响应: " . json_encode($result, JSON_UNESCAPED_UNICODE));
                return;
            }
            $this->release(10);
        }
        Log::info('创建客户队列 user_id：' . $this->user_id . '，响应：' . json_encode($result, JSON_UNESCAPED_UNICODE));
    }
}
